﻿using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace CuaHangBanhNgotBeoBeo.Game.ViewModel {
    public class MainViewModel : BaseViewModel {

        public static int SIZE_HOLE_WH = 80;
        public static int HOLE_MARGIN = 5;

        public override void LoadData() {
            GenerateHole();
        }

        private List<HoleViewModel> _holeViewModels = null;

        public List<HoleViewModel> HoleViewModels {
            get {
                if (_holeViewModels == null) {
                    _holeViewModels = new List<HoleViewModel>();
                }
                return _holeViewModels;
            }
        }

        private HoleViewModel[,] map = null;

        public HoleViewModel[,] Map {
            get { return map; }
            set { map = value; }
        }

        private int _colNum = 0;

        public int ColNum {
            get { return _colNum; }
            set { _colNum = value; }
        }

        private int _rowNum = 0;

        public int RowNum {
            get { return _rowNum; }
            set { _rowNum = value; }
        }

        private void GenerateHole() {

            // Fake json decoding

            List<String> map = new List<string>() { 
                "2|3|4|5|6",
                "2|3",
                "2|3|4",
                "2",
                "0|1|2",
                "0|2"
            };

            _colNum = map.Max(t => int.Parse(t.Split(new char[] { '|' }).Last())) + 1;
            _rowNum = map.Count;

            this.map = new HoleViewModel[_rowNum, _colNum];

            for (int i = 0; i < _rowNum; i++) {
                for (int j = 0; j < _colNum; j++) {
                    this.map[i, j] = null;
                }
            }

            double x = 0, y = 0;

            // Process each line
            for (int i = 0; i < map.Count; i++) {

                // Calc the y coord

                y = i * (MainViewModel.SIZE_HOLE_WH - MainViewModel.HOLE_MARGIN);

                // Get list of hole
                List<String> holes = map[i].Split(new char[] { '|' }).ToList();


                var test = map[i].Split(new char[] { '|' });

                foreach (String str in holes) {

                    int j = int.Parse(str);

                    // Calc x
                    x = j * (MainViewModel.SIZE_HOLE_WH + MainViewModel.HOLE_MARGIN) - (map.Count + 1) * MainViewModel.SIZE_HOLE_WH / 4.0f;

                    // New a HoleViewModel and add to _holeViewModels
                    var holeViewModel = new HoleViewModel();
                    holeViewModel.TopPosition = y;
                    holeViewModel.LeftPosition = x + i * MainViewModel.SIZE_HOLE_WH / 2.0f;
                    holeViewModel.IPos = i;
                    holeViewModel.JPos = j;

                    this.HoleViewModels.Add(holeViewModel);

                    this.map[i, j] = holeViewModel;
                    //holeView.Width = holeView.Height = MainViewModel.SIZE_HOLE_WH;
                    //holeView.SetValue(Canvas.TopProperty, y);
                    //holeView.SetValue(Canvas.LeftProperty, x + i * MainViewModel.SIZE_HOLE_WH / 2.0f);
                    //holeView.SetValue(Canvas.ZIndexProperty, 0);
                    //canvasMain.Children.Add(holeView);
                    //holeView.Name = "hole(" + i.ToString() + "," + j.ToString() + ")";
                }
            }

        }

    }
}
